package cate.game.pay.msg;

import cate.common.table.d.GDObj;
import cate.common.table.d.GDProp;
import cate.common.table.pay.PayItemRow;
import cp.solution.business.pay.dto.PayDeliverResult;
import cp.solution.business.pay.PayRecordEntity;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBPay;
import cate.game.pay.PayDeliverParam;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cp.solution.define.Lang;
import cp.solution.define.PayOrderType;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import easy.java.util.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
@ClientMsg(title = "使用代币支付 の 请求")
public class PayWithTokenMoneyReq implements MsgInput {

    @NoteField("支付项模板ID")
    public int itemTid;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        h(game, cc.getRole());
    }

    @Override
    public MsgConfig conf() {
        return MsgIdBPay.PAY_WITH_TOKEN_MONEY_REQ;
    }

    public void h(GameBody game, Role role) {
        if(!role.getBase().actor.enabled) {
            game.notice.message(role, "未授权");
            return;
        }
        PayItemRow pir = game.table.pay.item.get(itemTid);
        if(pir == null) {
            game.notice.message(role, "找不到ID="+this.itemTid+"的支付项");
            return;
        }
        long tokenNum = pir.price/100;
        MixRes cost = MixRes.buildSimple(GDObj.Type.PROP, GDProp.TID.TOKEN_MONEY, tokenNum);
        EcResult<MixResItem> chk = cost.consumeCheck(role);
        if(!chk.ok()) {
            game.notice.message(role, "代币不够："+chk.message);
            return;
        }
        cost.consumeDirect(role);
        log.info("{} 使用代币 {} 元 购买 ({}) {}", role, tokenNum, pir.id, pir.nameCn);

        long uid = new SnowflakeIdWorker(game.config.srvId%31, game.config.srvId%31).nextId();
        PayRecordEntity record = new PayRecordEntity();
        record.setPlatformOrderId("TK"+uid);
        record.setItemTid(itemTid);
        record.setItemNum(1);
        record.setPayCents(pir.price);
        record.setRoleId(role.getUid());
        record.setSrvId(role.getBase().srvId);
        record.setPayTime(new Date());
        record.setCreateTime(new Date());
        record.setOrderType(PayOrderType.DEVELOP);
        record.setGameId(role.getBase().gameId);
        record.setChannelCode(role.getBase().channelCode);
        record.setLangCode(Lang.zh_CN.value);
        record.setItemName(pir.nameCn);
        record.setAccountId(role.getBase().accountId);
        record.setUid(uid);
        try {
            PayDeliverResult r = role.getPay().deliver(record, PayDeliverParam.normal);
            game.notice.message(role, "已执行" + r.getBusinessComment());
        } catch (Exception e) {
            log.error("执行兑现支付异常", e);
            game.notice.message(role, e);
        }
    }

}
